package com.labyrinth.core;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.*;
import com.badlogic.gdx.graphics.VertexAttributes.Usage;

public class Button extends GameObject
{

	Button()
	{
	}

	Button(float x, float y)
	{
		super(x, y);
		this.setScale(50,100);
		setDrawType(GL10.GL_TRIANGLE_STRIP);
        setTexture("data/button/test_font/buttonexit2.png");

		setMesh();
	}

    public Button(float x, float y, String Texture)
	{
		super(x, y);
		this.setScale(50,100);
		setDrawType(GL10.GL_TRIANGLE_STRIP);
        setTexture(Texture);
		setMesh();
	}

	@Override
	public void setMesh()
	{
		mesh = new Mesh(true, 4, 4, new VertexAttribute(Usage.Position, 3, "a_position"), new VertexAttribute(Usage.TextureCoordinates, 2, "a_texCoord"));
		mesh.setVertices(new float[] {
                -0.5f, -0.5f, 0, 0, 1,
                0.5f, -0.5f, 0, 1, 1,
                -0.5f, 0.5f, 0, 0, 0,
                0.5f, 0.5f, 0, 1, 0 });
		mesh.setIndices(new short[] { 0, 1, 2, 3 });
		vertexCount = 4;

	}

	public void setTexture(String texturePath)
	{
		FileHandle textureFileHandle = Gdx.files.internal(texturePath);
		texture = new Texture(textureFileHandle);
	}

    public boolean clicked(float X,float Y)
    {
        float BX = this.getX();
        float BY = this.getY();

        if((X <= BX+this.getScaleX()/2)&& (X > BX-this.getScaleX()/2))
        {
         if((Y <= BY+this.getScaleY()/2)&& (Y > BY-this.getScaleY()/2))
         {
             return false;
         }
        }
        return true;
    }
    public void draw()
	{
		Gdx.gl10.glPushMatrix();
		Gdx.gl10.glTranslatef(position.getX(), position.getY(), 0);
		Gdx.gl10.glScalef(scaleX, scaleY, 0);
		Gdx.graphics.getGL10().glEnable(GL10.GL_TEXTURE_2D);
        Gdx.graphics.getGL10().glEnable(GL10.GL_ALPHA_TEST);
        Gdx.graphics.getGL10().glAlphaFunc(GL10.GL_GREATER, 0.5f);
        

		texture.bind();
		mesh.render(drawType, 0, vertexCount);
		Gdx.gl10.glPopMatrix();
	}

}
